package com.WaVel.service.impl;

import com.Emplyment.dto.ReUserPasswordDto;
import com.Emplyment.entity.UserMessage;
import com.Emplyment.exception.BaseException;
import com.Emplyment.exception.RegisterException;
import com.WaVel.mapper.UserMapper;
import com.WaVel.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    /**
     * 查询用户信息
     * @param id
     * @return
     */
    @Override
    public UserMessage userMessageResult(Long id) {
        if (id == null){
            throw new RegisterException("查询不到此用户");
        }
        UserMessage userMessage = userMapper.SelectEmplyeeMessage(id);
        return userMessage;
    }


    /**
     * 修改用户信息
     * @param dto
     */
    @Override
    public void updateUser(UserMessage dto) {
        userMapper.updateUser(dto);
    }



    /**
     * 修改图片信息
     * @param img
     */
    @Override
    public void updateAvatar(String img,Long id) {
        userMapper.updateAvatar(img,id);
    }


    /**
     * 修改密码
     * @param dto
     */
    @Override
    public void updatePassword(ReUserPasswordDto dto) {
        String username = dto.getUsername();
        UserMessage userMessage = userMapper.selectUserByUsername(dto.getUsername());
        String password = userMessage.getPassword();
        String oldPassword = DigestUtils.md5DigestAsHex(dto.getOldPassword().getBytes());
        if (!oldPassword.equals(password)){
            throw new BaseException("原密码错误");
        }
        String confirmPassword = DigestUtils.md5DigestAsHex(dto.getConfirmPassword().getBytes());
        userMapper.updatePassword(username,confirmPassword);
    }
}
